package com.bonc.rms.service.impl.tms;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



















import com.bonc.odk.logmanager.LogManager;
import com.bonc.rms.dao.tms.ZShortDimissionDao;
import com.bonc.rms.entity.tms.Template;
import com.bonc.rms.service.tms.ZEmployeeService;

/**
 * ZEmployeeServiceImpl
 * @author zlq
 *
 */
@Service
@Transactional
public class ZEmployeeServiceImpl implements ZEmployeeService{
	
	@Autowired
	ZShortDimissionDao zShortDimissionDao;


	/**
	 * 人员按学历和部门分布
	 */
	@Override
	public List<Template> getDegreeDistribute(Integer departmentId) {
		
		List<Template> list = new LinkedList<>();
	    Template template1 = new Template("本科", zShortDimissionDao.getDegreeDistribute(departmentId,"本科"));
	    Template template2 = new Template("硕士", zShortDimissionDao.getDegreeDistribute(departmentId,"硕士"));
	    Template template3 = new Template("博士", zShortDimissionDao.getDegreeDistribute(departmentId, "博士"));
	    Template template4 = new Template("专科及以下", zShortDimissionDao.getDegreeDistribute(departmentId, "other"));
	    list.add(template1);
	    list.add(template2);
	    list.add(template3);
	    list.add(template4);
		return list;
	}

	/**
	 * 性别分布
	 */
	@Override
	public List<Template> getGenderDistribute(Integer departmentId) {
		
		List<Template> list = new LinkedList<>();
		Template template1 = new Template("男", zShortDimissionDao.getGenderDistribute(departmentId, "男"));
		Template template2 = new Template("女", zShortDimissionDao.getGenderDistribute(departmentId, "女"));
		list.add(template1);
		list.add(template2);
		return list;
	}

	/**
	 * 年龄分布
	 */
	/**

	@Override
	public List<Template> getAgeDistribute() {

		List<Template> list = new LinkedList<>();
		Template template1 = new Template("50后",zTalentRepository.findByAge("5"));
		Template template2 = new Template("60后", zTalentRepository.findByAge("6"));
		Template template3 = new Template("70后", zTalentRepository.findByAge("7"));
		Template template4 = new Template("80后", zTalentRepository.findByAge("8"));
		Template template5 = new Template("90后", zTalentRepository.findByAge("9"));
		list.add(template1);
		list.add(template2);
		list.add(template3);
		list.add(template4);
		list.add(template5);
		return list;
	}
	 * 
	 */

	/**
	 * 工作年限分布
	 */
	/**

	@Override
	public List<Template> getWorkExperienceDistribute() {

		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String now = sdf.format(date);
		
		List<Template> list = new LinkedList<>();
		Template template1 = new Template("小于1年", zTalentRepository.findByWorkExperience(now, 0, 1*365));
		Template template2 = new Template("1-3年", zTalentRepository.findByWorkExperience(now, 1*365, 3*365));
		Template template3 = new Template("3-5年", zTalentRepository.findByWorkExperience(now, 3*365, 5*365));
		Template template4 = new Template("5-10年", zTalentRepository.findByWorkExperience(now, 5*365, 10*365));
		Template template5 = new Template("大于10年", zTalentRepository.findByWorkExperience(now, 10*365, 100*365));
		list.add(template1);
		list.add(template2);
		list.add(template3);
		list.add(template4);
		list.add(template5);
		return list;
	}
	 * 
	 */

	/**
	 * HC指标
	 * 处理日期范围
	 * 
	 * @param startMonth
	 * @param endMonth
	 */
	private void dealDate(Date startMonth, Date endMonth) {
		if(startMonth != null) {
			Calendar calStartMonth = Calendar.getInstance();
			calStartMonth.setTime(startMonth);
			calStartMonth.set(Calendar.DAY_OF_MONTH, 1);
			startMonth = calStartMonth.getTime();
		}
		
		if(endMonth != null) {
			Calendar calEndMonth = Calendar.getInstance();
			calEndMonth.setTime(endMonth);
			calEndMonth.set(Calendar.DAY_OF_MONTH, calEndMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
			endMonth = calEndMonth.getTime();
		}
	}
	
	/**
	 * HC指标
	 * 获取两日期之间的月份列表
	 * 
	 * @param startMonth
	 * @param endMonth
	 * @return
	 */
	private List<String> getMonthList(Date startMonth, Date endMonth) {
		
		Calendar startMonthCal = Calendar.getInstance();
		startMonthCal.setTime(startMonth);
		
		Calendar endMonthCal = Calendar.getInstance();
		endMonthCal.setTime(endMonth);
		
		List<String> monthList = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		while(!endMonthCal.equals(startMonthCal)) {
			monthList.add(sdf.format(startMonthCal.getTime()));
			startMonthCal.add(Calendar.MONTH, 1);
		}
		monthList.add(sdf.format(startMonthCal.getTime()));
		
		return monthList;
	}
	
	
	/**
	 * HC指标
	 * 补全某些缺失的月份(这是一种很LOW的方法，最好是在查询数据时与月份进行外连接查询)
	 * 
	 * @param data
	 * @param startMonth
	 * @param endMonth
	 */
	private void fillDate(Map<String, Integer> data, Date startMonth, Date endMonth) {
		
		List<String> monthList = getMonthList(startMonth, endMonth);
		for(int i = 0; i < monthList.size(); i ++) {
			if(!data.containsKey(monthList.get(i))) {
				data.put(monthList.get(i), 0);
			}
		}
	}	
	

	/**
	 * HC指标
	 * 获取入职类型人数列表
	 * entryType = 1 正式
	 * entryType = 2 实习 按 0.5 计算
	 */
	@Override
	public List<String> getEntryTypeList(Integer departmentId, 
										 Integer entryType, 
										 Date startMonth, 
										 Date endMonth) {
		
		dealDate(startMonth, endMonth);
		List<Map<String, String>> rsList =
				zShortDimissionDao.getEntryType(departmentId, entryType, startMonth, endMonth);
		
		Map<String, Integer> rsMap = new HashMap<String, Integer>();
		
		for (int i = 0; i < rsList.size(); i++) {			
			Map<String, String> map = rsList.get(i);
			String month = String.valueOf(map.get("MONTH"));
			try {
				Integer num = new Integer(String.valueOf(map.get("SUM")));
				rsMap.put(month, num);
			} catch (Exception e) {
				e.printStackTrace();
			}			
		}
		
		fillDate(rsMap, startMonth, endMonth);
				
		List<String> list = new ArrayList<String>();

		List<String> monthList = getMonthList( startMonth, endMonth);
		
		for(int i = 0; i < monthList.size(); i ++) {
			int num = rsMap.get(monthList.get(i));
			if (entryType == 1) {
				list.add(String.valueOf(num));
			}else {
				list.add(String.valueOf(num * 0.5));
			}

		}
		return list;
	}

	
	/**
	 * 获取两日期间所有星期五的日期列表
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public List<String> getFridayList(Date startDate, Date endDate){
			
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(startDate);		
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endDate);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		List<String> fridayList = new ArrayList<String>();
		/**
		 * Calendar.DAY_OF_WEEK 
		 * 星期日：1    星期一：2    星期二：3   星期四：4    星期五：5   星期五：6
		 */
		while(!endCal.equals(startCal)) {			
			if(startCal.get(Calendar.DAY_OF_WEEK) == 6 ){
				fridayList.add(sdf.format(startCal.getTime()));				
			}
			startCal.add(Calendar.DATE, 1);      // 日期增加一天
		}
		if (startCal.get(Calendar.DAY_OF_WEEK) == 6) {
			fridayList.add(sdf.format(startCal.getTime()));  //判断结束日期是否为星期五
		}
		//System.out.println("fridayList:"+fridayList);
		return fridayList;
	}
	
	/**
	 * 获取周五日期对应的上一个周六日期
	 * 即 周六 - 周五为一周
	 * @param fridayList
	 * @return
	 */
	private List<String> getSaturday(List<String> fridayList){
		
		List<String> lastSaturdayList = new ArrayList<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < fridayList.size(); i++) {
			
			// String -> Date
			String nextFridayString = fridayList.get(i);
			Date nextFridayDate = new Date();
			try {
				nextFridayDate = sdf.parse(nextFridayString);
			}catch (ParseException e) {
				e.printStackTrace();
			}			
			Calendar nextFridayCal = Calendar.getInstance();
			nextFridayCal.setTime(nextFridayDate);
			nextFridayCal.add(Calendar.DATE, -6);
			lastSaturdayList.add(sdf.format(nextFridayCal.getTime()));
		}	
		//System.out.println("saturdayList:"+lastSaturdayList);
		return lastSaturdayList;
	}
	
	
	/**
	 * HC周报
	 * 获取入职类型对应人数列表
	 * entry_type = 1
	 * 正式
	 * entryType = 2 
	 * 实习，按 0.5 计算
	 */
	public List<String> getEntryTypeNumByWeek(Integer departmentId, 
										    Integer entryType, 
										    Date startDate, 
										    Date endDate){
		
		List<String> fridayList = getFridayList(startDate, endDate);
		List<String> lastSaturdayList = getSaturday(fridayList);
		List<String> numList = new ArrayList<String>();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					
		for (int i = 0; i < fridayList.size(); i++) {
			
			Date fridayDate = new Date();
			Date lastSaturdayDate = new Date();
			
			try {
				fridayDate = sdf.parse(fridayList.get(i));
				lastSaturdayDate = sdf.parse(lastSaturdayList.get(i));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			List<Map<String, String>> rsList = 
					zShortDimissionDao.getEntryTypeByDate(departmentId, entryType, lastSaturdayDate, fridayDate);
			
			for (int j = 0; j < rsList.size(); j++) {
				Map<String, String> map = rsList.get(j);
				try {
					String sumString = String.valueOf(map.get("sum"));
					Integer sumInteger = new Integer(sumString);
					if (entryType != 1) {
						numList.add(String.valueOf(sumInteger * 0.5));
					}else {
						numList.add(String.valueOf(sumInteger));
					}
				} catch (NumberFormatException  e) {
					e.printStackTrace();
				}

			}		
		}
		//System.out.println(fridayList);
		//System.out.println(lastSaturdayList);
			//System.out.println("internNumList"+internNumList);
		return numList;
		} 

	/**
	 * 短期离职员工分布
	 * 按性别和部门
	 * getShortDimissionDistributeByGender
	 */
	@Override
	public List<Template> getSDDByGender(Integer departmentId) {
		
		List<Template> list = new ArrayList<Template>();
		Template template1 = new Template("男", zShortDimissionDao.getShortDimissionByGender(departmentId, "男") );
		Template template2 = new Template("女", zShortDimissionDao.getShortDimissionByGender(departmentId, "女") );
		list.add(template1);
		list.add(template2);
		return list;
	}
	
		
	/**
	 * 短期离职员工分布
	 * 按年龄和部门
	 * 50后、60后、70后、80后、90后
	 * getShortDimissionDistributeByAge
	 */
	@Override
	public List<Template> getSDDByAge(Integer departmentId) {
		
		List<Template> list = new LinkedList<>();
		Template template1 = new Template("50后",zShortDimissionDao.getShortDimissionByAge(departmentId, "5"));
		Template template2 = new Template("60后",zShortDimissionDao.getShortDimissionByAge(departmentId, "6"));
		Template template3 = new Template("70后",zShortDimissionDao.getShortDimissionByAge(departmentId, "7"));
		Template template4 = new Template("80后",zShortDimissionDao.getShortDimissionByAge(departmentId, "8"));
		Template template5 = new Template("90后",zShortDimissionDao.getShortDimissionByAge(departmentId, "9"));
		list.add(template1);
		list.add(template2);
		list.add(template3);
		list.add(template4);
		list.add(template5);
		return list;
	}
	

	/**
	 * 短期离职员工分布
	 * 按学位和部门
	 * getShortDimissionDistributeByAge
	 */
	@Override
	public List<Template> getSDDByDegree(Integer departmentId) {
				
		List<Template> list = new LinkedList<>();
	    Template template1 = new Template("本科", zShortDimissionDao.getShortDimissionByDegree(departmentId, "本科"));
	    Template template2 = new Template("硕士", zShortDimissionDao.getShortDimissionByDegree(departmentId, "硕士"));
	    Template template3 = new Template("博士", zShortDimissionDao.getShortDimissionByDegree(departmentId, "博士"));
	    Template template4 = new Template("专科及以下", zShortDimissionDao.getShortDimissionByDegree(departmentId, "other"));
	    list.add(template1);
	    list.add(template2);
	    list.add(template3);
	    list.add(template4);
		return list;
	}
	
	

	/**
	 * 短期离职员工分布
	 * 按工作年限和部门
	 * getShortDimissionDistributeByWorkExperience
	 */
	@Override
	public List<Template> getSDDByWE(Integer departmentID) {
		
		List<Template> list = new LinkedList<>();
		Template template1 = new Template("小于1年", zShortDimissionDao.getShortDimissionByWE(departmentID, 0, 365));
		Template template2 = new Template("1-3年", zShortDimissionDao.getShortDimissionByWE(departmentID, 1*365, 3*365));
		Template template3 = new Template("3-5年", zShortDimissionDao.getShortDimissionByWE(departmentID, 3*365, 5*365));
		Template template4 = new Template("5-10年", zShortDimissionDao.getShortDimissionByWE(departmentID, 5*365, 10*365));
		Template template5 = new Template("大于10年", zShortDimissionDao.getShortDimissionByWE(departmentID, 10*365, 100*365));
		list.add(template1);
		list.add(template2);
		list.add(template3);
		list.add(template4);
		list.add(template5);
		return list;
	}

	/**
	 * 短期离职员工数量前十的一级部门部门(总公司)
	 */
	@Override
	public Map<String, List<String>> getShortDimissionTOPDept() {
		
		LogManager.Method("Int Service : ZEmployeeServiceImpl -> getShortDimissionTOPDept");
		Map<String, List<String>> rsMap = new HashMap<String, List<String>>();
		List<Map<String, String>> list = zShortDimissionDao.getShortDimissionTOPDept();
		List<String> deptNameList = new ArrayList<>();
		List<String> deptNumList = new ArrayList<>();
		
 		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = list.get(i);
			deptNameList.add(map.get("name"));
			deptNumList.add(String.valueOf(map.get("sum")));
		}
 		
 		rsMap.put("name", deptNameList);
 		rsMap.put("num", deptNumList);
 		
		LogManager.Method("deptNameList" + deptNameList);
		LogManager.Method("deptNumList" + deptNumList);
		LogManager.Method("Out Service : ZEmployeeServiceImpl -> getShortDimissionTOPDept");		
		return rsMap;
	}

	/**
	 * 短期离职员工数量前十的二级部门(某一级部门下)
	 */
	@Override
	public Map<String, List<String>> getShortDimissionTOPDeptByDept(Integer departmentId) {
		
		LogManager.Method("In Service : ZEmployeeServiceImpl -> getShortDimissionTOPDeptByDept");
		Map<String, List<String>> rsMap = new HashMap<String, List<String>>();
		List<Map<String, String>> list = zShortDimissionDao.getShortDimissionTOPDeptByDept(departmentId);
		List<String> deptNameList = new ArrayList<>();
		List<String> deptNumList = new ArrayList<>();
		
 		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = list.get(i);
			deptNameList.add(map.get("name"));
			deptNumList.add(String.valueOf(map.get("sum")));
		}
 		
 		rsMap.put("name", deptNameList);
 		rsMap.put("num", deptNumList);
		
 		LogManager.Method("deptNameList" + deptNameList);
 		LogManager.Method("deptNumList" + deptNumList);
 		LogManager.Method("Out Service : ZEmployeeServiceImpl -> getShortDimissionTOPDeptByDept");
		return rsMap;
	}

	/**
	 * 判断登录用户所属一级部门是否为叶子结点 1-是   0-否
	 */
	@Override
	public Integer getLeafTag(Integer departmentId) {
		
		return zShortDimissionDao.getLeafTag(departmentId);
	}

	/**
	 * 判断登录用户所在一级部们所属level
	 */
	@Override
	public Integer getLevelTag(Integer departmentId) {
		
		return zShortDimissionDao.getLevelTag(departmentId);
	}

	/**
	 * 获取部门 id 对应的部门名称
	 */
	@Override
	public String getDeptName(Integer departmentId) {
		
		return zShortDimissionDao.getDeptName(departmentId);
	}

	/**
	 * 获取登录用户所在部门的子部门数量
	 */
	@Override
	public Integer getSubDeptNum(Integer departmentId) {
		
		LogManager.Method("In Service : ZEmployeeServiceImpl -> getSubDeptNum");
		LogManager.Method("SubDeptNum = " + zShortDimissionDao.getSubDeptNum(departmentId));
		LogManager.Method("Out Service : ZEmployeeServiceImpl -> getSubDeptNum");
		return zShortDimissionDao.getSubDeptNum(departmentId);
	}


	
}
